﻿using System.Text.Json.Serialization;

namespace ApiData.Common
{
    /// <summary>
    /// 返回数据实体
    /// </summary>
    public class ResultData
    {
        /// <summary>
        /// 常量代码
        /// </summary>
        public enum ConstantCode
        {
            成功 = 0,
            成功弹窗 = 1,
            失败 = 2,
            失败弹窗 = 3,
            登录过期 = 4
        }

        /// <summary>
        /// 消息
        /// </summary>
        public string msg { get; set; }
        /// <summary>
        /// 是否成功
        /// </summary>
        public bool state { get; set; }
        /// <summary>
        /// 消息代码
        /// </summary>
        public int code { get; set; }
        /// <summary>
        /// 数据
        ///// </summary>
        public object? data { get; set; }
        /// <summary>
        /// 数量（仅用于分页）
        /// </summary>
        public int count { get; set; }

        /// <summary>
        /// 构造函数
        /// [msg:string.Empty|state:false|code:ConstantCode.result_fail|data:null]
        /// </summary>
        public ResultData()
        {
            msg = string.Empty;
            state = false;
            code = (int)ConstantCode.失败;
            data = null;
            count = 0;
        }

        /// <summary>
        /// 返回成功的结果
        /// </summary>
        /// <param name="_msg">提示消息</param>
        public ResultData Success(string _msg = "成功")
        {
            msg = _msg;
            data = null;
            code = (int)ConstantCode.成功;
            state = true;
            count = 0;
            return this;
        }

        /// <summary>
        /// 返回成功的结果，并设置为弹窗消息
        /// </summary>
        /// <param name="_msg">提示消息</param>
        /// <returns></returns>
        public ResultData SuccessAlert(string _msg = "成功")
        {
            msg = _msg;
            data = null;
            code = (int)ConstantCode.成功弹窗;
            state = true;
            count = 0;
            return this;
        }

        /// <summary>
        /// 返回成功的数据
        /// </summary>
        /// <param name="_data">需要返回的数据</param>
        /// <param name="_msg">提示消息</param>
        /// <returns></returns>
        public ResultData SuccessData(object? _data = null, string _msg = "成功")
        {
            msg = _msg;
            data = _data;
            code = (int)ConstantCode.成功;
            state = true;
            count = 0;
            return this;
        }

        /// <summary>
        /// 返回成功的数据
        /// </summary>
        /// <param name="_count">总数据量</param>
        /// <param name="_data">需要返回的数据</param>
        /// <param name="_msg">提示消息</param>
        /// <returns></returns>
        public ResultData SuccessData(int _count, object? _data = null, string _msg = "成功")
        {
            msg = _msg;
            data = _data;
            code = (int)ConstantCode.成功;
            state = true;
            count = _count;
            return this;
        }

        /// <summary>
        /// 返回失败的结果
        /// </summary>
        /// <param name="_msg">提示消息</param>
        /// <returns></returns>
        public ResultData Fail(string _msg = "失败")
        {
            msg = _msg;
            data = null;
            code = (int)ConstantCode.失败;
            state = false;
            count = 0;
            return this;
        }

        /// <summary>
        /// 返回失败的结果，并设置为弹窗消息
        /// </summary>
        /// <param name="_msg">提示消息</param>
        /// <returns></returns>
        public ResultData FailAlert(string _msg = "失败")
        {
            msg = _msg;
            data = null;
            code = (int)ConstantCode.失败弹窗;
            state = false;
            count = 0;
            return this;
        }

        /// <summary>
        /// 返回登录过期的结果
        /// </summary>
        /// <param name="_msg">提示消息</param>
        /// <returns></returns>
        public ResultData OutTime(string _msg = "登录过期")
        {
            msg = _msg;
            data = null;
            code = (int)ConstantCode.登录过期;
            state = false;
            count = 0;
            return this;
        }
    }
}
